<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="generator" content="pandoc">
    <title>Software Carpentry: Programming with Python</title>
    <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
    <link rel="stylesheet" type="text/css" href="css/swc.css" />
    <link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
    <meta charset="UTF-8" />
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
  </head>
  <body class="lesson">
    <div class="container card">
      <div class="banner">
        <a href="http://software-carpentry.org" title="Software Carpentry">
          <img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
        </a>
      </div>
      <article>
      <div class="row">
        <div class="col-md-10 col-md-offset-1">
                    <a href="index.html"><h1 class="title">Programming with Python</h1></a>
          <h2 class="subtitle">Reference</h2>
          <h2 id="analyzing-patient-data"><a href="01-numpy.html">Analyzing Patient Data</a></h2>
<ul>
<li>Import a library into a program using <code>import libraryname</code>.</li>
<li>Use the <code>numpy</code> library to work with arrays in Python.</li>
<li>Use <code>variable = value</code> to assign a value to a variable in order to record it in memory.</li>
<li>Variables are created on demand whenever a value is assigned to them.</li>
<li>Use <code>print(something)</code> to display the value of <code>something</code>.</li>
<li>The expression <code>array.shape</code> gives the shape of an array.</li>
<li>Use <code>array[x, y]</code> to select a single element from an array.</li>
<li>Array indices start at 0, not 1.</li>
<li>Use <code>low:high</code> to specify a slice that includes the indices from <code>low</code> to <code>high-1</code>.</li>
<li>All the indexing and slicing that works on arrays also works on strings.</li>
<li>Use <code># some kind of explanation</code> to add comments to programs.</li>
<li>Use <code>array.mean()</code>, <code>array.max()</code>, and <code>array.min()</code> to calculate simple statistics.</li>
<li>Use <code>array.mean(axis=0)</code> or <code>array.mean(axis=1)</code> to calculate statistics across the specified axis.</li>
<li>Use the <code>pyplot</code> library from <code>matplotlib</code> for creating simple visualizations.</li>
</ul>
<h2 id="repeating-actions-with-loops"><a href="02-loop.html">Repeating Actions with Loops</a></h2>
<ul>
<li>Use <code>for variable in collection</code> to process the elements of a collection one at a time.</li>
<li>The body of a for loop must be indented.</li>
<li>Use <code>len(thing)</code> to determine the length of something that contains other values.</li>
</ul>
<h2 id="storing-multiple-values-in-lists"><a href="03-lists.html">Storing Multiple Values in Lists</a></h2>
<ul>
<li><code>[value1, value2, value3, ...]</code> creates a list.</li>
<li>Lists are indexed and sliced in the same way as strings and arrays.</li>
<li>Lists are mutable (i.e., their values can be changed in place).</li>
<li>Strings are immutable (i.e., the characters in them cannot be changed).</li>
</ul>
<h2 id="analyzing-data-from-multiple-files"><a href="04-files.html">Analyzing Data from Multiple Files</a></h2>
<ul>
<li>Use <code>glob.glob(pattern)</code> to create a list of files whose names match a pattern.</li>
<li>Use <code>*</code> in a pattern to match zero or more characters, and <code>?</code> to match any single character.</li>
</ul>
<h2 id="making-choices"><a href="05-cond.html">Making Choices</a></h2>
<ul>
<li>Use the <code>ImageGrid</code> class from the <code>ipythonblocks</code> library to create simple “images” made of colored blocks.</li>
<li>Specify colors use (red, green, blue) triples, each component of which is an integer in the range 0..255.</li>
<li>Use <code>if condition</code> to start a conditional statement, <code>elif condition</code> to provide additional tests, and <code>else</code> to provide a default.</li>
<li>The bodies of the branches of conditional statements must be indented.</li>
<li>Use <code>==</code> to test for equality.</li>
<li><code>X and Y</code> is only true if both X and Y are true.</li>
<li><code>X or Y</code> is true if either X or Y, or both, are true.</li>
<li>Zero, the empty string, and the empty list are considered false; all other numbers, strings, and lists are considered true.</li>
<li>Nest loops to operate on multi-dimensional data.</li>
<li>Put code whose parameters change frequently in a function, then call it with different parameter values to customize its behavior.</li>
</ul>
<h2 id="creating-functions"><a href="06-func.html">Creating Functions</a></h2>
<ul>
<li>Define a function using <code>def name(...params...)</code>.</li>
<li>The body of a function must be indented.</li>
<li>Call a function using <code>name(...values...)</code>.</li>
<li>Numbers are stored as integers or floating-point numbers.</li>
<li>Integer division produces the whole part of the answer (not the fractional part).</li>
<li>Each time a function is called, a new stack frame is created on the <strong>call stack</strong> to hold its parameters and local variables.</li>
<li>Python looks for variables in the current stack frame before looking for them at the top level.</li>
<li>Use <code>help(thing)</code> to view help for something.</li>
<li>Put docstrings in functions to provide help for that function.</li>
<li>Specify default values for parameters when defining a function using <code>name=value</code> in the parameter list.</li>
<li>Parameters can be passed by matching based on name, by position, or by omitting them (in which case the default value is used).</li>
</ul>
<h2 id="errors-and-exceptions"><a href="07-errors.html">Errors and Exceptions</a></h2>
<ul>
<li>Tracebacks can look intimidating, but they give us a lot of useful information about what went wrong in our program, including where the error occurred and what type of error it was.</li>
<li>An error having to do with the “grammar” or syntax of the program is called a <code>SyntaxError</code>. If the issue has to do with how the code is indented, then it will be called an <code>IndentationError</code>.</li>
<li>A <code>NameError</code> will occur if you use a variable that has not been defined (either because you meant to use quotes around a string, you forgot to define the variable, or you just made a typo).</li>
<li>Containers like lists and dictionaries will generate errors if you try to access items in them that do not exist. For lists, this type of error is called an <code>IndexError</code>; for dictionaries, it is called a <code>KeyError</code>.</li>
<li>Trying to read a file that does not exist will give you an <code>IOError</code>. Trying to read a file that is open for writing, or writing to a file that is open for reading, will also give you an <code>IOError</code>.</li>
</ul>
<h2 id="defensive-programming"><a href="08-defensive.html">Defensive Programming</a></h2>
<ul>
<li>Program defensively, i.e., assume that errors are going to arise, and write code to detect them when they do.</li>
<li>Put assertions in programs to check their state as they run, and to help readers understand how those programs are supposed to work.</li>
<li>Use preconditions to check that the inputs to a function are safe to use.</li>
<li>Use postconditions to check that the output from a function is safe to use.</li>
<li>Write tests before writing code in order to help determine exactly what that code is supposed to do.</li>
</ul>
<h2 id="debugging"><a href="09-debugging.html">Debugging</a></h2>
<ul>
<li>Know what code is supposed to do <em>before</em> trying to debug it.</li>
<li>Make it fail every time.</li>
<li>Make it fail fast.</li>
<li>Change one thing at a time, and for a reason.</li>
<li>Keep track of what you’ve done.</li>
<li>Be humble.</li>
</ul>
<h2 id="command-line-programs"><a href="10-cmdline.html">Command-Line Programs</a></h2>
<ul>
<li>The <code>sys</code> library connects a Python program to the system it is running on.</li>
<li>The list <code>sys.argv</code> contains the command-line arguments that a program was run with.</li>
<li>Avoid silent failures.</li>
<li>The “file” <code>sys.stdin</code> connects to a program’s standard input.</li>
<li>The “file” <code>sys.stdout</code> connects to a program’s standard output.</li>
</ul>
<h2 id="glossary">Glossary</h2>
<dl>
<dt><span id="additive-color-model">additive color model</span></dt>
<dd>A way to represent colors as the sum of contributions from primary colors such as <a href="#rgb">red, green, and blue</a>.
</dd>
<dt><span id="argument">argument</span></dt>
<dd>A value given to a function or program when it runs. The term is often used interchangeably (and inconsistently) with <a href="#parameter">parameter</a>.
</dd>
<dt><span id="assertion">assertion</span></dt>
<dd>An expression which is supposed to be true at a particular point in a program. Programmers typically put assertions in their code to check for errors; if the assertion fails (i.e., if the expression evaluates as false), the program halts and produces an error message. See also: <a href="#invariant">invariant</a>, <a href="#precondition">precondition</a>, <a href="#postcondition">postcondition</a>.
</dd>
<dt><span id="assign">assign</span></dt>
<dd>To give a value a name by associating a variable with it.
</dd>
<dt><span id="body">body</span></dt>
<dd>(of a function): the statements that are executed when a function runs.
</dd>
<dt><span id="call-stack">call stack</span></dt>
<dd>A data structure inside a running program that keeps track of active function calls.
</dd>
<dt><span id="case-insensitive">case-insensitive</span></dt>
<dd>Treating text as if upper and lower case characters of the same letter were the same. See also: <a href="#case-sensitive">case-sensitive</a>.
</dd>
<dt><span id="case-sensitive">case-sensitive</span></dt>
<dd>Treating text as if upper and lower case characters of the same letter are different. See also: <a href="#case-insensitive">case-insensitive</a>.
</dd>
<dt><span id="comment">comment</span></dt>
<dd>A remark in a program that is intended to help human readers understand what is going on, but is ignored by the computer. Comments in Python, R, and the Unix shell start with a <code>#</code> character and run to the end of the line; comments in SQL start with <code>--</code>, and other languages have other conventions.
</dd>
<dt><span id="compose">compose</span></dt>
<dd>To apply one function to the result of another, such as <code>f(g(x))</code>.
</dd>
<dt><span id="conditional-statement">conditional statement</span></dt>
<dd>A statement in a program that might or might not be executed depending on whether a test is true or false.
</dd>
<dt><span id="comma-separated-values">comma-separated values</span></dt>
<dd>(CSV) A common textual representation for tables in which the values in each row are separated by commas.
</dd>
<dt><span id="default-value">default value</span></dt>
<dd>A value to use for a <a href="#parameter">parameter</a> if nothing is specified explicitly.
</dd>
<dt><span id="defensive-programming">defensive programming</span></dt>
<dd>The practice of writing programs that check their own operation to catch errors as early as possible.
</dd>
<dt><span id="delimiter">delimiter</span></dt>
<dd>A character or characters used to separate individual values, such as the commas between columns in a <a href="#comma-separated-values">CSV</a> file.
</dd>
<dt><span id="docstring">docstring</span></dt>
<dd>Short for “documentation string”, this refers to textual documentation embedded in Python programs. Unlike comments, docstrings are preserved in the running program and can be examined in interactive sessions.
</dd>
<dt><span id="documentation">documentation</span></dt>
<dd>Human-language text written to explain what software does, how it works, or how to use it.
</dd>
<dt><span id="dotted-notation">dotted notation</span></dt>
<dd>A two-part notation used in many programming languages in which <code>thing.component</code> refers to the <code>component</code> belonging to <code>thing</code>.
</dd>
<dt><span id="empty-string">empty string</span></dt>
<dd>A character string containing no characters, often thought of as the “zero” of text.
</dd>
<dt><span id="encapsulation">encapsulation</span></dt>
<dd>The practice of hiding something’s implementation details so that the rest of a program can worry about <em>what</em> it does rather than <em>how</em> it does it.
</dd>
<dt><span id="floating-point-number">floating-point number</span></dt>
<dd>A number containing a fractional part and an exponent. See also: <a href="#integer">integer</a>.
</dd>
<dt><span id="for-loop">for loop</span></dt>
<dd>A loop that is executed once for each value in some kind of set, list, or range. See also: <a href="#while-loop">while loop</a>.
</dd>
<dt><span id="function-call">function call</span></dt>
<dd>A use of a function in another piece of software.
</dd>
<dt><span id="immutable">immutable</span></dt>
<dd>Unchangeable. The value of immutable data cannot be altered after it has been created. See also: <a href="#mutable">mutable</a>.
</dd>
<dt><span id="import">import</span></dt>
<dd>To load a <a href="#library">library</a> into a program.
</dd>
<dt><span id="in-place-operators">in-place operators</span></dt>
<dd>An operator such as <code>+=</code> that provides a shorthand notation for the common case in which the variable being assigned to is also an operand on the right hand side of the assignment. For example, the statement <code>x += 3</code> means the same thing as <code>x = x + 3</code>.
</dd>
<dt><span id="index">index</span></dt>
<dd>A subscript that specifies the location of a single value in a collection, such as a single pixel in an image.
</dd>
<dt><span id="inner-loop">inner loop</span></dt>
<dd>A loop that is inside another loop. See also: <a href="#outer-loop">outer loop</a>.
</dd>
<dt><span id="integer">integer</span></dt>
<dd>A whole number, such as -12343. See also: <a href="#floating-point-number">floating-point number</a>.
</dd>
<dt><span id="invariant">invariant</span></dt>
<dd>An expression whose value doesn’t change during the execution of a program, typically used in an <a href="#assertion">assertion</a>. See also: <a href="#precondition">precondition</a>, <a href="#postcondition">postcondition</a>.
</dd>
<dt><span id="library">library</span></dt>
<dd>A family of code units (functions, classes, variables) that implement a set of related tasks.
</dd>
<dt><span id="loop-variable">loop variable</span></dt>
<dd>The variable that keeps track of the progress of the loop.
</dd>
<dt><span id="member">member</span></dt>
<dd>A variable contained within an <a href="#object">object</a>.
</dd>
<dt><span id="method">method</span></dt>
<dd>A function which is tied to a particular <a href="#object">object</a>. Each of an object’s methods typically implements one of the things it can do, or one of the questions it can answer.
</dd>
<dt><span id="object">object</span></dt>
<dd>A collection of conceptually related variables (<a href="#member">members</a>) and functions using those variables (<a href="#method">methods</a>).
</dd>
<dt><span id="outer-loop">outer loop</span></dt>
<dd>A loop that contains another loop. See also: <a href="#inner-loop">inner loop</a>.
</dd>
<dt><span id="parameter">parameter</span></dt>
<dd>A variable named in the function’s declaration that is used to hold a value passed into the call. The term is often used interchangeably (and inconsistently) with <a href="#argument">argument</a>.
</dd>
<dt><span id="pipe">pipe</span></dt>
<dd>A connection from the output of one program to the input of another. When two or more programs are connected in this way, they are called a “pipeline”.
</dd>
<dt><span id="postcondition">postcondition</span></dt>
<dd>A condition that a function (or other block of code) guarantees is true once it has finished running. Postconditions are often represented using <a href="#assertion">assertions</a>.
</dd>
<dt><span id="precondition">precondition</span></dt>
<dd>A condition that must be true in order for a function (or other block of code) to run correctly.
</dd>
<dt><span id="regression">regression</span></dt>
<dd>To re-introduce a bug that was once fixed.
</dd>
<dt><span id="return-statement">return statement</span></dt>
<dd>A statement that causes a function to stop executing and return a value to its caller immediately.
</dd>
<dt><span id="rgb">RGB</span></dt>
<dd>An <a href="#additive-color-model">additive model</a> that represents colors as combinations of red, green, and blue. Each color’s value is typically in the range 0..255 (i.e., a one-byte integer).
</dd>
<dt><span id="sequence">sequence</span></dt>
<dd>A collection of information that is presented in a specific order. For example, in Python, a <a href="#string">string</a> is a sequence of characters, while a list is a sequence of any variable.
</dd>
<dt><span id="shape">shape</span></dt>
<dd>An array’s dimensions, represented as a vector. For example, a 5×3 array’s shape is <code>(5,3)</code>.
</dd>
<dt><span id="silent-failure">silent failure</span></dt>
<dd>Failing without producing any warning messages. Silent failures are hard to detect and debug.
</dd>
<dt><span id="slice">slice</span></dt>
<dd>A regular subsequence of a larger sequence, such as the first five elements or every second element.
</dd>
<dt><span id="stack-frame">stack frame</span></dt>
<dd>A data structure that provides storage for a function’s local variables. Each time a function is called, a new stack frame is created and put on the top of the <a href="#call-stack">call stack</a>. When the function returns, the stack frame is discarded.
</dd>
<dt><span id="standard-input">standard input</span></dt>
<dd>A process’s default input stream. In interactive command-line applications, it is typically connected to the keyboard; in a <a href="#pipe">pipe</a>, it receives data from the <a href="#standard-output">standard output</a> of the preceding process.
</dd>
<dt><span id="standard-output">standard output</span></dt>
<dd>A process’s default output stream. In interactive command-line applications, data sent to standard output is displayed on the screen; in a <a href="#pipe">pipe</a>, it is passed to the <a href="#standard-input">standard input</a> of the next process.
</dd>
<dt><span id="string">string</span></dt>
<dd>Short for “character string”, a <a href="#sequence">sequence</a> of zero or more characters.
</dd>
<dt><span id="syntax-error">syntax error</span></dt>
<dd>A programming error that occurs when statements are in an order or contain characters not expected by the programming language.
</dd>
<dt><span id="test-oracle">test oracle</span></dt>
<dd>A program, device, data set, or human being against which the results of a test can be compared.
</dd>
<dt><span id="test-driven-development">test-driven development</span></dt>
<dd>The practice of writing unit tests <em>before</em> writing the code they test.
</dd>
<dt><span id="traceback">traceback</span></dt>
<dd>The sequence of function calls that led to an error.
</dd>
<dt><span id="tuple">tuple</span></dt>
<dd>An <a href="#immutable">immutable</a> <a href="#sequence">sequence</a> of values.
</dd>
<dt><span id="type">type</span></dt>
<dd>The classification of something in a program (for example, the contents of a variable) as a kind of number (e.g. <a href="#float">floating-point</a>, <a href="#integer">integer</a>), <a href="#string">string</a>, or something else.
</dd>
<dt><span id="type-of-error">type of error</span></dt>
<dd>Indicates the nature of an error in a program. For example, in Python, an <code>IOError</code> to problems with file input/output. See also: <a href="#syntax-error">syntax error</a>.
</dd>
<dt><span id="while-loop">while loop</span></dt>
<dd>A loop that keeps executing as long as some condition is true. See also: <a href="#for-loop">for loop</a>.
</dd>
</dl>
        </div>
      </div>
      </article>
      <div class="footer">
        <a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
        <a class="label swc-blue-bg" href="https://github.com/swcarpentry/python-novice-inflammation">Source</a>
        <a class="label swc-blue-bg" href="mailto:admin@software-carpentry.org">Contact</a>
        <a class="label swc-blue-bg" href="LICENSE.html">License</a>
      </div>
    </div>
    <!-- Javascript placed at the end of the document so the pages load faster -->
    <script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
    <script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
  </body>
</html>
